package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import com.itheima.health.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Transactional
@Service(interfaceClass = MemberService.class)
public class MemberServiceImpl implements MemberService {
    @Autowired
    MemberDao memberDao;

    @Autowired
    OrderDao orderDao;


    @Override
    public Integer find(Member member) {
        return memberDao.find(member);
    }

    @Override
    public void add(Member member) {
    memberDao.add(member);
    }

    @Override
    public Map<String, Object> getMemberReport() {
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> monthList = new ArrayList<>();
        ArrayList<Integer> countList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-12);
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH,1);
            String s = new SimpleDateFormat("yyyy-MM").format(calendar.getTime());
            monthList.add(s);
        }
        map.put("months",monthList);
        for (String month : monthList) {
            month=month+"-31";
            Integer count=memberDao.getCountByDate(month);
            countList.add(count);
        }
        map.put("memberCount",countList);
        return map;
    }

    @Override
    public Map<String, Object> getBusinessReportData() throws Exception {
        HashMap<String, Object> map = new HashMap<>();
        //当前时间
        String today = DateUtils.parseDate2String(new Date());
         //本周第一天
        String thisWeekFirstDate = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //本周最后一天
        String thisWeekLastDate = DateUtils.parseDate2String(DateUtils.getSundayOfThisWeek());
        //本月第一天
        String thisMonthFirstDate = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
        //本月最后一天
        String thisMonthLastDate = DateUtils.parseDate2String(DateUtils.getLastDay4ThisMonth());
        map.put("reportDate", today);
        //当前时间(今日)新增会员
        Integer todayNewMember=memberDao.getTodayNewMember(today);
        map.put("todayNewMember",todayNewMember);
        //总会员数
        Integer totalMember = memberDao.getTotalMember();
        map.put("totalMember",totalMember);
        //本周新增会员
        HashMap<String, String> weekmap = new HashMap<>();
        weekmap.put("first",thisWeekFirstDate);
        weekmap.put("end",thisWeekLastDate);
        Integer thisWeekNewMember=memberDao.getthisWeekNewMember(weekmap);
        map.put("thisWeekNewMember",thisWeekNewMember);
        //本月新增会员
        HashMap<String, String> monthmap = new HashMap<>();
        monthmap.put("first",thisMonthFirstDate);
        monthmap.put("end",thisMonthLastDate);
        Integer thisMonthNewMember = memberDao.getthisWeekNewMember(monthmap);
        map.put("thisMonthNewMember",thisMonthNewMember);
        //今日预约人数
        Integer todayOrderNumber = orderDao.gettodayOrderNumber(today);
        map.put("todayOrderNumber",todayOrderNumber);
        //今日到诊人数
        Integer todayVisitsNumber=orderDao.gettodayVisitsNumber(today);
        map.put("todayVisitsNumber",todayVisitsNumber);
        //本周预约人数
        Integer thisWeekOrderNumber = orderDao.getthisWeekOrderNumber(weekmap);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);
        //本周到诊人数
        Integer thisWeekVisitsNumber = orderDao.getthisWeekVisitsNumber(thisWeekFirstDate);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        //本月预约人数
        Integer thisMonthOrderNumber = orderDao.getthisWeekOrderNumber(monthmap);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);
        //本月到诊人数
        Integer thisMonthVisitsNumber = orderDao.getthisWeekVisitsNumber(thisMonthFirstDate);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        //热门套餐
        List<Map<String,Object>> hotSetmeal = orderDao.gethotSetmeal();
        map.put("hotSetmeal",hotSetmeal);
        return map;
    }
}
